package is202.hrms.web;

import is202.hrms.ejb.FileEJB;
import is202.hrms.ejb.ModulenEJB;
import is202.hrms.ejb.ProgressEJB;
import is202.hrms.ejb.UserEJB;
import is202.hrms.entity.Modulen;
import is202.hrms.entity.Progid;
import is202.hrms.entity.Progress;
import is202.hrms.entity.StoredFile;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.myfaces.custom.fileupload.UploadedFile;

/**
 * The backing bean for progressartment.xhtml. The Named annotation marks it as
 * a CDI backing bean. The ConversationScoped annotation controls the lifecycle
 * of the bean. The lifetime of a ConversationScoped bean is explicitly managed
 * in the code.
 *
 * @author even
 */
@Named("progressbean")
@ConversationScoped
public class ProgressBean implements Serializable {

    // The @Inject annotation is a request to the server to
    // give us a Conversation object, and store it in conv
    @Inject
    private Conversation conv;
    @EJB
    ProgressEJB progressEjb;
    @EJB
    ModulenEJB modulenEjb;
    @EJB
    UserEJB userEjb;
    private Progress progress;
    private boolean updating;
    private static String visible = "none";
    private Progress par;
    // Progresse properties - These properties will be copied to a Progresse
    // object, when the time comes to save the data in the database.
    private int progressId;
    private int studentId;
    private List<Progress> progressen;

    public ProgressBean() {
    }

    public String getVisible() {
        return visible;
    }

    public void setVisible(String visible) {
        this.visible = visible;
    }

    public List<Progress> getProgressen() {
        return progressen;
    }

    public void setProgressen(List<Progress> progressen) {
        this.progressen = progressen;
    }

    public Progress getProgress() {
        return progress;
    }

    public Progress getProgress2(int student, int modulid) {
        Progid p = new Progid(student, modulid);
        progress = progressEjb.find(p);
        return progress;
    }

    public void setProgress(Progress progress) {
        this.progress = progress;
    }

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    /**
     * called when the progress selects a progressartment from the list in
     * progressartments.xhtml.
     *
     * @param studentId
     */
    public void setProgressId(int studentId, int modulenId) {

        this.studentId = studentId;
        if (conv.isTransient()) {

            conv.begin();
        }
        Progid p = new Progid(studentId, modulenId);

        progress = progressEjb.find(p);

        if (null == progress) {
            // we will get here if progressId is not a valid, or if
            // progressId is valid but there is no progressartment with that progressno
            progress = new Progress();
            progress.setBruker(userEjb.find(studentId));

            progress.setModuler(modulenEjb.find(modulenId));
            updating = false;
            progressId = 0;

            // name = "";
            //   employees = new ArrayList<Employee>();
        } else {
            updating = true;
            par = progress;
            //  employees = progress.getEmployees();
        }
        saveProgress();
        show();
    }

    public Progress getPar() {
        return par;
    }

    public void setPar(Progress par) {
        this.par = par;
    }

    public int getProgressId() {
        return progressId;
    }

    public boolean isUpdating() {
        return updating;
    }

    private Progress createProgressObject() {
        // Create a progressartment object containing the data
        // from the form. The object can be passed on to
        // progressListBean

        return progress;
    }

    public String saveProgress() {

        if (progressId > 0) {
            progressEjb.update(progress);
        } else {
            progressEjb.insert(progress);
        }

        conv.end();
        System.out.println(conv.isTransient());
        return "me.xhtml";
    }

    public View deleteProgress() {
        progress = createProgressObject();
        progressEjb.delete(progress);
        conv.end();

        return View.department;
    }

    public boolean checkProgress(int studentId, int modulenId) {

        this.studentId = studentId;

        Progid p = new Progid(studentId, modulenId);

        progress = progressEjb.find(p);
        Modulen m = modulenEjb.find(modulenId);

        if (null != progress) {
            return true;

        }
        if (m.timeOutBool(m.getDato())) {
            return true;
        }

        return false;

    }

    public String checkDelivered(int studentId, int modulenId) {

        if (checkProgress(studentId, modulenId)) {
            if (progress == null) {
                return "Tiden er ute";
            }
            return "Du har levert modul " + modulenId;
        }
        return "Du har ikke levert modul " + modulenId;
    }

    public String giveMeClass(int studentId, int modulenId) {
        if (checkProgress(studentId, modulenId)) {
            if (progress == null) {
                //returnere en annen css klasse hvis du vil
                return "isa_error";
            }
            return "isa_success";
        }
        return "isa_warning";

    }

    public void show() {
        if (visible.equals("none")) {
            visible = "block";
        } else {
            visible = "none";
        }
        System.out.println(visible);
    }

}
